﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace libSyllabus.Model
{
    /// <summary>
    /// This is Section entity class, which represents an entry in the Sections table.
    /// </summary>
    /// <remarks>
    /// For now, Sections table contains following columns:
    ///  - ID               : Auto Generated Index (Number)
    ///  - CourseID      : ID of the parent Course (Number)
    ///  - InstructorID  : ID of the Section Instructor (Number) !!Instructors Table Not Added
    ///  - TypeID         : Section Type (Number)
    ///  
    /// Property Change Notification is taken care of in the base class
    /// </remarks>
    [Table(Name = "Sections")]
    public sealed class SectionEntity : EntityBase
    {
        #region Constants

        // Column Name Constants
        public const string SECTION_ID = "ID";
        public const string SECTION_COURSEID = "CourseID";
        public const string SECTION_INSTRUCTORID = "InstructorID";
        public const string SECTION_TYPEID = "TypeID";

        #endregion

        #region Constructors

        public SectionEntity()
        {
            m_classes = new EntitySet<ClassEntity>();
            m_course = new EntityRef<CourseEntity>();
            m_type = new EntityRef<SectionTypeEntity>();
        }

        #endregion

        #region Fields

        // Fields mapped to database
        [Column(Name = SECTION_ID, IsPrimaryKey = true, CanBeNull = false, 
            IsDbGenerated = true, DbType = "INT NOT NULL Identity", AutoSync = AutoSync.OnInsert)]
        private Int32 m_id;

        [Column(Name = SECTION_COURSEID, CanBeNull = false)]
        private Int32 m_courseID;

        [Column(Name = SECTION_INSTRUCTORID, CanBeNull = false)]
        private Int32 m_instructorID;

        [Column(Name = SECTION_TYPEID, CanBeNull = false)]
        private Int32 m_typeID;

        // Mapped Course
        private EntityRef<CourseEntity> m_course;

        //private EntityRef<UserEntity> m_instructor;

        // Mapped Type
        private EntityRef<SectionTypeEntity> m_type;

        // Mapped Classes
        private EntitySet<ClassEntity> m_classes;

        #endregion

        #region Properties

        // Encapsulation
        public Int32 ID
        {
            get
            {
                return m_id;
            }
        }

        public Int32 CourseID
        {
            get
            {
                return m_courseID;
            }
        }

        public Int32 InstructorID
        {
            get
            {
                return m_instructorID;
            }
            set
            {
                if (m_instructorID != value)
                {
                    RaisePropertyChanging(SECTION_INSTRUCTORID);
                    m_instructorID = value;
                    RaisePropertyChanged(SECTION_INSTRUCTORID);
                }
            }
        }

        public Int32 TypeID
        {
            get
            {
                return m_typeID;
            }
            set
            {
                if (m_typeID != value)
                {
                    RaisePropertyChanging(SECTION_TYPEID);
                    m_typeID = value;
                    RaisePropertyChanged(SECTION_TYPEID);
                }
            }
        }

        [Association(Storage = "m_course", ThisKey = SECTION_COURSEID)]
        public CourseEntity Course
        {
            get
            {
                return m_course.Entity;
            }
        }

        [Association(Storage = "m_type", ThisKey = SECTION_TYPEID)]
        public SectionTypeEntity Type
        {
            get
            {
                return m_type.Entity;
            }
        }

        [Association(Storage = "m_classes", OtherKey = ClassEntity.CLASS_SECTIONID)]
        public EntitySet<ClassEntity> Classes
        {
            get
            {
                return m_classes;
            }
        }

        #endregion
    }
}
